<template>
  <div class="countdown-wrap">
    <span :endTime="endTime" :callback="callback" :endText="endText">
      <slot>
        <span v-html="content" :class="{'endText-span': content==endText}">{{content}}</span>
      </slot>
    </span>
  </div>
</template>
<script>
export default {
  name: "Countdown",
  data() {
    return {
      content: "",
    };
  },
  props: {
    typeData: String,
    endTime: {
      type: Number,
      default: 0,
    },
    endText: {
      type: String,
      default: "代理进行中",
    },
    callback: {
      type: Function,
      default: "",
    },
  },
  mounted() {
    // console.log(this.$route.path);
    if (this.endTime) {
      // console.log("开抢时间", this.endTime);
      this.countdowm(this.endTime);
      // this.countdowm(1597458236);
    }
  },
  methods: {
    countdowm(timestamp) {
      let self = this;
      let timer = setInterval(function () {
        let nowTime = new Date();
        let endTime = new Date(timestamp * 1000);
        // let t = endTime.getTime() - nowTime.getTime();
        timestamp--;
        let t = timestamp * 1000;
        if (t > 0) {
          let day = Math.floor(t / 86400000);
          let hour = Math.floor((t / 3600000) % 24);
          let min = Math.floor((t / 60000) % 60);
          let sec = Math.floor((t / 1000) % 60);
          hour = hour < 10 ? "0" + hour : hour;
          min = min < 10 ? "0" + min : min;
          sec = sec < 10 ? "0" + sec : sec;
          let format = "";
          if (day > 0) {
            format = `${day}天${hour}小时${min}分${sec}秒`;
          }
          if (day <= 0 && hour > 0) {
            format = `${hour}小时${min}分${sec}秒`;
          }
          if (day <= 0 && hour <= 0) {
            format = `${min}分${sec}秒`;
          }

          // console.log("format", format);
          // self.content = format;
          // console.log(self.typeData);
          if (self.typeData == "list") {
            // console.log(day,hour);
            if (day > 0) {
              self.content =
                "距离代理开始： <span class='color-span'>" +
                day +
                "</span>天<span class='color-span'>" +
                hour +
                "</span>小时<span class='color-span'>" +
                min +
                "</span>分<span class='color-span'>" +
                sec +
                "</span>秒";
            }
            if (day <= 0 && hour > 0) {
              self.content =
                "距离代理开始： <span class='color-span'>" +
                hour +
                "</span>小时<span class='color-span'>" +
                min +
                "</span>分<span class='color-span'>" +
                sec +
                "</span>秒";
            }
            if (day <= 0 && hour <= 0) {
              self.content =
                "距离代理开始：<span class='color-span'>" +
                min +
                "</span>分<span class='color-span'>" +
                sec +
                "</span>秒";
            }
          } else {
            if (day > 0) {
              self.content =
                "距离代理<span class='wbg-span'>" +
                day +
                "</span>天<span class='wbg-span'>" +
                hour +
                "</span>时<span class='wbg-span'>" +
                min +
                "</span>分<span class='wbg-span'>" +
                sec +
                "</span>秒开始";
            }
            if (day <= 0 && hour > 0) {
              self.content =
                "距离代理<span class='wbg-span'>" +
                hour +
                "</span>时<span class='wbg-span'>" +
                min +
                "</span>分<span class='wbg-span'>" +
                sec +
                "</span>秒开始";
            }
            if (day <= 0 && hour <= 0) {
              self.content =
                "距离代理<span class='wbg-span'>" +
                min +
                "</span>分<span class='wbg-span'>" +
                sec +
                "</span>秒开始";
            }
          }
        } else {
          clearInterval(timer);
          self.content = self.endText;
          if (self.$route.path == "/goods_detail") {
            self._callback();
          }
        }
      }, 1000);
    },
    _callback() {
      if (this.callback && this.callback instanceof Function) {
        console.log("callback");
        this.callback(this);
      }
    },
  },
};
</script>
<style lang="scss">
.countdown-wrap {
  letter-spacing: 4px;
  .endText-span {
    color: #fe6018;
    letter-spacing: 6px;
  }
}
.color-span {
  color: #fd5f18;
}
.wbg-span {
  display: inline-block;
  width: 42px;
  height: 42px;
  line-height: 42px;
  background: rgba(255, 255, 255, 1);
  border-radius: 4px;
  font-size: 26px;
  font-weight: 500;
  color: rgba(254, 96, 24, 1);
  margin: 0 12px;
}
</style>